﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameName1
{
    /// <summary>
    /// Class that handles the map
    /// </summary>
    class Map
    {
        private List<Texture2D> _tilesTextures;
        private int _tileHeight;
        private int _tileWidth;
        private int[,] _map;

        public int TileHeight
        {
            get { return _tileHeight; }
            set { _tileHeight = (int)MathHelper.Clamp(value, 16, 128); }
        }
        
        public int TileWidth
        {
            get { return _tileWidth; }
            set { _tileWidth = (int)MathHelper.Clamp(value, 16, 128); }
        }

        public int TileCountX
        {
            get { return _map.GetLength(1); }
        }

        public int TileCountY
        {
            get { return _map.GetLength(0); }
        }

        public Map()
        {
            _tilesTextures = new List<Texture2D>();
        }

        public void GenerateMap()
        {
            _map = new int[,] 
            {
                {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
                {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},
                {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
                {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
                {0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
                {0,0,0,0,1,0,0,2,2,2,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
                {0,0,0,0,1,0,0,2,0,2,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0},
                {0,0,0,0,1,0,0,2,0,2,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
                {0,0,0,0,1,0,0,2,2,2,0,0,0,1,0,0,2,2,2,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
                {0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,2,0,2,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
                {0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,2,2,2,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
                {0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
                {0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,2,2,2,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,2,0,2,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},
                {0,0,0,0,0,0,0,2,2,2,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0},
                {0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0}
            };
        }

        public void DrawMap(SpriteBatch spriteBatch, Vector2 cameraPosition)
        {
            for (int i = 0; i < TileCountX; i++)
            {
                for (int j = 0; j < TileCountY; j++)
                {
                    spriteBatch.Draw(
                    GetTileTextureByIndex(_map[j, i]),
                    new Rectangle(
                        i * TileWidth - (int)cameraPosition.X,
                        j * TileHeight - (int)cameraPosition.Y,
                        TileWidth,
                        TileHeight),
                    null,
                    Color.White);
                }
            }
        }

        public void AddTileTextures(ContentManager contentManager, params string[] textureNames) 
        {
            foreach (string textureName in textureNames)
            {
                _tilesTextures.Add(contentManager.Load<Texture2D>(textureName));
            }
        }

        public Texture2D GetTileTextureByIndex(int index)
        {
            return _tilesTextures[index];
        }

        public void SetTileDimensions(int width, int height)
        {
            TileWidth = width;
            TileHeight = height;
        }
    }
}